package com.cetsk.android.talkorigins;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;

public class MainActivity extends FragmentActivity {
	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Must be created programmatically rather than auto-inflated to
		// handle backstack functionality
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		ClaimListFragment listFragment = new ClaimListFragment();
		fragmentTransaction.add(R.id.claim_list_fragment, listFragment);

		
		if (findViewById(R.id.claim_view_fragment) != null && findViewById(R.id.claim_view_fragment).getVisibility() == View.VISIBLE) {
			ClaimViewFragment viewFragment = new ClaimViewFragment();
			fragmentTransaction.add(R.id.claim_view_fragment, viewFragment);
			
		}
		fragmentTransaction.commit();
		
		
		dialog = ProgressDialog.show(this, "Loading",
				"Building database. Please wait....", true);
		new Thread() {
			public void run() {
				try {
					new HTMLParse(getApplicationContext());
				} catch (Exception e) {
					
				}
				handler.sendEmptyMessage(0);
				dialog.dismiss();
			}
		}.start();
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			WebView wv = (WebView) findViewById(R.id.webview);
			if (wv != null) {
				wv.loadData("<h1>Placeholder</h1>", "text/html", "UTF-8");
			}

		}
	};
	/*
	 * @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if
	 * (keyCode == KeyEvent.KEYCODE_BACK) { return false; } else { return
	 * super.onKeyDown(keyCode, event); } }
	 */
}